import 'dart:collection';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:meePet/utils/ImageUtil.dart';

import '../../manager/RouteManager.dart';
import '../ImageUtils.dart';

extension StringExt on String? {
  bool isNulOrEmpty() {
    return (this == null ||
        this!.isEmpty ||
        this == "null" ||
        this == "" ||
        this == " ");
  }

  String orEmpty() {
    if (isNulOrEmpty()) {
      return "";
    } else {
      return this!;
    }
  }

  String orDef(String str) {
    if (isNulOrEmpty()) {
      return str;
    } else {
      return this!;
    }
  }

  String toHttpScheme() {
    if (isNulOrEmpty()) {
      return "";
    } else {
      return this!.replaceFirst(
          RouteManager.getAppScheme(), RouteManager.getAppHttpScheme());
    }
  }

  ///获取url里的参数key-value，以？和&还有=号进行分割处理
  Map<String, String> urlGetParams() {
    Map<String, String> params = HashMap();
    if (isNulOrEmpty()) {
      return params;
    } else {
      var strs = this!.split("?");
      if (strs.length >= 2) {
        var strs1 = strs[1].split("&");
        strs1.forEach((element) {
          var strs2 = element.split("=");
          if (strs.length >= 2) {
            params[strs2.elementAt(0)] = strs2.elementAt(1);
          }
        });
      }
    }
    return params;
  }

  ///uri解码
  String toUrlDecode() {
    if (isNulOrEmpty()) {
      return "";
    } else {
      return Uri.decodeFull(this!);
    }
  }

  ///uri编码
  String toUrlEncode() {
    if (isNulOrEmpty()) {
      return "";
    } else {
      return Uri.encodeFull(this!);
    }
  }
}

extension String2Ext on String {
  CachedNetworkImage loadImage(
      {double? w, double? h, BoxFit? fit = BoxFit.cover}) {
    return ImageUtils.showNetImageWh(this, width: w, height: h, mFit: fit);
  }

  String img() {
    return ImageUtil.getImg(this);
  }

  String webp() {
    return ImageUtil.getImgWebp(this);
  }

  String jpg() {
    return ImageUtil.getImgJpg(this);
  }

  String png() {
    return ImageUtil.getImgPng(this);
  }

  Image loadImageRes(double w, double h) {
    return Image(
      image: AssetImage(this),
      fit: BoxFit.cover,
      height: h,
      width: w,
    );
  }

  Image imageRes({double? w, double? h}) {
    return Image(
      image: AssetImage(this),
      fit: BoxFit.cover,
      height: h,
      width: w,
    );
  }

  String omit(int len, {String? replaceStr = "..."}) {
    if (isNulOrEmpty()) {
      return "";
    }
    if (length > len) {
      return this.substring(0, len) + replaceStr.orEmpty();
    } else {
      return this;
    }
  }

  String reversed() {
    String input = this;
    return input.split('').reversed.join();
  }
}
